package com.readingtree.library.security;


import com.readingtree.library.domain.LibraryUser;
import com.readingtree.library.domain.SecurityUser;
import com.readingtree.library.service.LibraryUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;


/**
 * Created by Administrator on 2017/4/10.
 */
@Component
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired  //业务服务类
    private LibraryUserService userService;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        LibraryUser user = userService.findByUsername(userName);
        if (user == null) {
            throw new UsernameNotFoundException("UserName " + userName + " not found");
        }
        // SecurityUser实现UserDetails并将SysUser的name映射为username
        SecurityUser securityUser = new SecurityUser(user);
        return  securityUser;
    }
}
